package cn.com.eado.crm.v.fragment


import android.util.Log
import androidx.fragment.app.Fragment
import android.view.View
import androidx.appcompat.app.AlertDialog
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import androidx.recyclerview.widget.LinearLayoutManager
import cn.com.eado.crm.R
import cn.com.eado.crm.app.extenstion.toast
import cn.com.eado.crm.databinding.FragmentQlhzBinding
import cn.com.eado.crm.model.LoadStatus
import cn.com.eado.crm.model.RetryCallback
import cn.com.eado.crm.v.adapter.RVCompanyAdapter
import cn.com.eado.crm.v.weiget.AutoSpinner
import cn.com.eado.crm.v.weiget.ClearEditText
import cn.com.eado.crm.v.weiget.DateSpinner
import cn.com.eado.crm.vm.CompanyViewModel
import cn.com.eado.preado.app.utils.autoCleared
import com.getbase.floatingactionbutton.FloatingActionButton
import kotlinx.android.synthetic.main.fragment_qlhz.*

/**
 * A simple [Fragment] subclass.
 */
class QLHZFragment : BaseBindingFragment<FragmentQlhzBinding>(R.layout.fragment_qlhz) {

    private var vm by autoCleared<CompanyViewModel>()
    private var adapter by autoCleared<RVCompanyAdapter>()
    private lateinit var title: String

    override fun initTheFragment() {
        initView()
        initViewModel()
        initBinding()
        loadData()
    }

    private fun initView() {
        initTitleBar()
        initOptionMenu()
        initRecyclerView()
    }

    private fun initTitleBar() {
        title = arguments?.getString("name")?:"XXX管理"
        titleBar5.setTitle(title)
    }

    private fun initOptionMenu(){
        fab_menu4.findViewById<FloatingActionButton>(R.id.fab_1).setOnClickListener {
            val layout = initAddQLHZ()
            val dialog = AlertDialog.Builder(activity!!)
                .setTitle("新增")
                .setView(layout)
                .setPositiveButton("提交", null)//结合下面的dialog.getButton，保证点击确定不关闭dialog
                .setNegativeButton("取消"){ dialog, pos -> }
                .show()
            dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener{//必须在show()之后
                if (checkQLHZ(layout)) dialog.dismiss()
            }
        }
    }

    private fun initAddQLHZ(): View {
        val layout = View.inflate(activity, R.layout.add_qlhz, null)
        val inputSpinner1 = layout.findViewById<AutoSpinner>(R.id.inputSpinner1)
        val dateSpinner = layout.findViewById<DateSpinner>(R.id.dateSpinner)
        val inputSpinner3 = layout.findViewById<AutoSpinner>(R.id.inputSpinner3)
        val inputSpinner4 = layout.findViewById<AutoSpinner>(R.id.inputSpinner4)
        inputSpinner1.setList(listOf<String>("aaa","bbb","ccc"))  //for test
        return layout
    }

    private fun checkQLHZ(layout: View): Boolean {
        val inputSpinner1 = layout.findViewById<AutoSpinner>(R.id.inputSpinner1)
        val inputSpinner2 = layout.findViewById<AutoSpinner>(R.id.inputSpinner2)
        val inputSpinner3 = layout.findViewById<AutoSpinner>(R.id.inputSpinner3)
        val inputSpinner4 = layout.findViewById<AutoSpinner>(R.id.inputSpinner4)
        val et1 = layout.findViewById<ClearEditText>(R.id.et1)
        //
        if (inputSpinner1.getText().isEmpty()){
            activity?.toast("请选择${inputSpinner1.getName()}")
            return false
        }
        if (inputSpinner2.getText().isEmpty()){
            activity?.toast("请选择${inputSpinner2.getName()}")
            return false
        }
        if (inputSpinner3.getText().isEmpty()){
            activity?.toast("请选择${inputSpinner3.getName()}")
            return false
        }
        if (inputSpinner4.getText().isEmpty()){
            activity?.toast("请选择${inputSpinner4.getName()}")
            return false
        }
        if (et1.text.trim().isEmpty()){
            activity?.toast("跟踪信息不能为空")
            return false
        }
        return true
    }


    private fun initRecyclerView() {
        recyclerView3.layoutManager = LinearLayoutManager(activity)
        val adapter = RVCompanyAdapter({ view, pos, bean ->  //click
            //
        },{ pos, bean ->  //long click
            //
        })
        recyclerView3.adapter = adapter
        this.adapter = adapter
    }

    private fun initViewModel(){
        vm = ViewModelProviders.of(this).get(CompanyViewModel::class.java)
//        vm.result.observe(this, Observer {
//            if (it.status == LoadStatus.SUCCESS){
////                Log.e("tag", "it=${it.data}")
//            }
//        })
    }

    private fun initBinding(){
        binding.lifecycleOwner = viewLifecycleOwner
//        binding.result = vm.result
        binding.callback = object : RetryCallback {
            override fun retry() {
                vm.refresh()
            }
        }
    }

    private fun loadData(){
        vm.setLoadCompany(true)
    }


}
